package org.NooLab.glue.net.ftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.NooLab.glue.components.pkg.DataDeliveryPackageIntf;
import org.NooLab.glue.net.UserData;
import org.NooLab.utilities.logging.PrintLog;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;



class FtpClientApaj {
	
	FTPClient ftpc ;
	InitialContext context;
	
	
	String targetAddress ="" ; // either hostname, or IP address
	int    targetPort = 21 ;   // default port 
	String initialDirectory = "" ;
	
	private boolean usePermanentLogin = true;
	private boolean loggedIn = false;
	UserData baseBoxUser ;
	
	int printLevel ;
	PrintLog out ;
	
	public FtpClientApaj( UserData basicuser, int printlevel) {
		
		
		baseBoxUser = basicuser;
		
		printLevel = printlevel ;
		out = new PrintLog(printLevel,true) ;
		
		ftpc = new FTPClient();
		
	}
		
	 
	
	public void setup(){
		
		/*
		 * get a initial context. By default the settings in the file
		 * jndi.properties are used. You can explicitly set up properties
		 * instead of using the file.
		 */
		Properties properties = new Properties();
		// properties.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
		// properties.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
		
		properties.put("java.naming.provider.url", "localhost");
		
		try{
			
			context = new InitialContext(properties);
			 
			
			
		} catch (NamingException e) {
			e.printStackTrace();
		}
		
	}
	
	public void permamentLogin( DataDeliveryPackageIntf ftpPkg ){
		
		try{
			if (ftpc.isConnected()==false){
				ftpc.connect( ftpPkg.getTargetAddress(), ftpPkg.getTargetPort() );
			}
		
			if (loggedIn==false){
				// ftpc.login("noolab", "glue");
				ftpc.login( baseBoxUser.getName(), baseBoxUser.getPassword());
			}
			loggedIn = true;
			usePermanentLogin = true;
		}catch(Exception e){
			
		}
		
	}
	
	public void disconnect(){

		try {
			if (loggedIn == true) {
				ftpc.logout();
				loggedIn = false ;
			}
			if (ftpc.isConnected() == false) {
				ftpc.disconnect();
			}
		} catch (Exception e) {

		}
	}
	
	public void send( DataDeliveryPackageIntf ftpPkg ){
		
		
		try {
			
											out.print(4, "client: sending start...  ") ;
			// ftpc.connect("localhost", 7100);
			if (ftpc.isConnected()==false){
				ftpc.connect( ftpPkg.getTargetAddress(), ftpPkg.getTargetPort() );
			}
			
			if (loggedIn==false){
			// ftpc.login("noolab", "glue");
				ftpc.login( baseBoxUser.getName(), baseBoxUser.getPassword() );
			}
			
			
			ftpc.setFileType(FTP.BINARY_FILE_TYPE);
			
			ftpc.enterLocalPassiveMode();
			// ftp.enterRemotePassiveMode();
			
			
			ftpc.setAutodetectUTF8(true);
			
			// String filename = "D:/dev/java/data/simple_test.xml";
			// ftpc.storeFile(filename, new FileInputStream(  filename));
			ftpc.storeFile( ftpPkg.getStorFilename(), ftpPkg.getDataAsInputStream() );
			

			// if we do not stream a long continuous story with small gaps, like a webcam ...
			if (usePermanentLogin==false){
				ftpc.logout();
				ftpc.disconnect();
			}
											out.print(4, "client: sending completed.") ;
			
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
